﻿<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:UserControls="clr-namespace:Finaltec.Hart.Analyzer.View.UserControls"
               xmlns:Converter="clr-namespace:Finaltec.Hart.Analyzer.View.Converter" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:exp="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
               xmlns:DataModels="clr-namespace:Finaltec.Hart.Analyzer.ViewModel.DataModels;assembly=Finaltec.Hart.Analyzer.ViewModel"
               x:Class="Finaltec.Hart.Analyzer.View.SendCommand" KeyUp="WindowKeyUp" MinHeight="160"
               MouseLeftButtonDown="WindowMouseLeftButtonDown" FocusManager.FocusedElement="{Binding ElementName=tbCommand}"
               Title="SendCommand" Loaded="WindowLoaded" Unloaded="WindowUnloaded" WindowStartupLocation="CenterOwner">

    <Window.Resources>
        <Converter:BooleanToOpacity x:Key="BooleanToOpacity" />
        <Converter:BooleanToVisibility x:Key="BooleanToVisibility" />
    </Window.Resources>

    <Window.Style>
        <StaticResource ResourceKey="DialogWindowStyle"/>
    </Window.Style>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Border BorderThickness="0,0,1,0" BorderBrush="{DynamicResource WindowBorder}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Grid Grid.Row="0" Grid.Column="0" Height="24" Opacity="0.6" Background="{StaticResource DefaultMenuBackground}">
                    <TextBlock Margin="5,1,0,1" Text="Send Command" TextTrimming="CharacterEllipsis" Background="Transparent" VerticalAlignment="Center" FontWeight="Bold" />
                </Grid>

                <Grid Margin="5" Grid.Row="1" Grid.Column="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Label Content="Command: " HorizontalAlignment="Right" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" />
                    <UserControls:ByteValidationTextBox Text="{Binding Command, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 
    			            HorizontalContentAlignment="Center" Grid.Row="0" Grid.Column="1" MaxLength="3" Width="50" VerticalAlignment="Center" HorizontalAlignment="Left" x:Name="tbCommand" 
    			            IsValid="{Binding CommandValid, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" GotFocus="CommandGotFocus">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="GotFocus">
                                <exp:CallMethodAction MethodName="GotFocusAction" TargetObject="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </UserControls:ByteValidationTextBox>

                    <StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="60,0">
                        <TextBlock Text="Press 'Tab' key to" Margin="0,-0.5" FontWeight="DemiBold" FontSize="10" FontFamily="Consolas" HorizontalAlignment="Center" />
                        <TextBlock Text="switch to the next entry" Margin="0,-0.5" FontWeight="DemiBold" FontSize="10" FontFamily="Consolas" HorizontalAlignment="Center" />
                    </StackPanel>
                    <StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10,0">
                        <TextBlock Text="Press 'Up' or 'Down' key" Margin="0,-0.5" FontWeight="DemiBold" FontSize="10" FontFamily="Consolas" HorizontalAlignment="Center" />
                        <TextBlock Text="to switch to change type" Margin="0,-0.5" FontWeight="DemiBold" FontSize="10" FontFamily="Consolas" HorizontalAlignment="Center" />
                    </StackPanel>

                    <Label Content="Data: " HorizontalAlignment="Right" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" IsEnabled="{Binding EnableData}" />

                    <Border CornerRadius="1" Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="{StaticResource WindowBorder}" Background="#88FFFFFF" Margin="5,2,2,2">
                        <ScrollViewer Margin="0,0,21,0" Width="350" Focusable="False" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Center">
                            <ListView Name="lvDataList" Background="Transparent" SelectedItem="{Binding SelectedRawValue}" Opacity="{Binding EnableData, Converter={StaticResource BooleanToOpacity}}" 
                                      IsEnabled="{Binding EnableData}" KeyboardNavigation.TabNavigation="Continue" ItemsSource="{Binding RawValues}" MouseDown="ListViewMouseDown">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="GotFocus">
                                        <exp:CallMethodAction MethodName="TextBoxGotFocusAction" TargetObject="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>

                                <ListView.ItemContainerStyle>
                                    <Style>
                                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
                                    </Style>
                                </ListView.ItemContainerStyle>
                                <ListView.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" />
                                    </ItemsPanelTemplate>
                                </ListView.ItemsPanel>

                                <ListView.ItemTemplate>
                                    <DataTemplate DataType="DataModels:Input">
                                        <TextBox GotFocus="TextBoxGotFocus" LostFocus="TextBoxLostFocus" Padding="-1.5,0" BorderBrush="Transparent" Margin="2,0"
                                                 Text="{Binding RawValue, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}">
                                            <TextBox.Style>
                                                <Style TargetType="{x:Type TextBox}">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding IsValid}" Value="False">
                                                            <Setter Property="ToolTip" Value="{Binding Path=DataContext.ErrorMessage, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                                                            <Setter Property="Background" Value="#88C90000" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBox.Style>
                                        </TextBox>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </ScrollViewer>
                    </Border>

                    <Button Content="x" FontFamily="Consolas" FontWeight="Bold" FontSize="16" Foreground="#FF888888" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" Height="14" Focusable="False"
                            Width="14" Margin="8,0" VerticalAlignment="Center" IsEnabled="{Binding EnableData}" Command="{Binding ClearDataCommand}">
                        <Button.Template>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border x:Name="Border" CornerRadius="12" BorderThickness="1" Background="#88FFFFFF" BorderBrush="#FF888888">
                                    <ContentPresenter Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" Height="21.5" />
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>

                    <Label Content="{Binding ErrorMessage, Mode=OneWay}" Visibility="{Binding ShowError, Mode=OneWay}" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" FontSize="11" 
    			           FontFamily="Consolas" FontWeight="Bold" Foreground="DarkRed" />
                </Grid>

                <StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
                    <Button Content="Send" Command="{Binding SendCommand}" IsDefault="True">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="GotFocus">
                                <exp:CallMethodAction MethodName="GotFocusAction" TargetObject="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                    <Button Content="Cancel" Command="{Binding CancelCommand}" IsCancel="True">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="GotFocus">
                                <exp:CallMethodAction MethodName="GotFocusAction" TargetObject="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                </StackPanel>
            </Grid>
        </Border>

        <Grid Grid.Column="1">

    		<Grid.RowDefinitions>
    			<RowDefinition Height="Auto" />
    			<RowDefinition Height="Auto" />
    		</Grid.RowDefinitions>

    		<Grid Grid.Row="0" Grid.Column="1" Height="24" Opacity="0.6" Background="{StaticResource DefaultMenuBackground}" Visibility="{Binding TypeChangeable, Converter={StaticResource BooleanToVisibility}}">
    			<TextBlock Margin="5,1,0,1" Text="Change Type" TextTrimming="CharacterEllipsis" Background="Transparent" VerticalAlignment="Center" FontWeight="Bold" />
    		</Grid>
    		<Grid Grid.Row="1" Grid.RowSpan="2" Grid.Column="1" Visibility="{Binding TypeChangeable, Converter={StaticResource BooleanToVisibility}}">
    			<StackPanel Margin="5,3">
    				<RadioButton Name="rbByte" KeyboardNavigation.IsTabStop="False" Content="Byte" GroupName="Type" Margin="0,1" IsChecked="{Binding Type.IsByte}" />
    				<StackPanel Orientation="Horizontal" Margin="0,1">
    					<RadioButton Name="rbInt16" KeyboardNavigation.IsTabStop="False" Content="Int16" GroupName="Type" IsChecked="{Binding Type.IsUShort}" />
    					<ToggleButton KeyboardNavigation.IsTabStop="False" Content="Signed" Height="16" FontSize="9" Margin="5,0" Padding="2,0" IsChecked="{Binding Type.IsShort}" />
    				</StackPanel>
    				<StackPanel Orientation="Horizontal" Margin="0,1">
    					<RadioButton Name="rbInt24" KeyboardNavigation.IsTabStop="False" Content="Int24" GroupName="Type" IsChecked="{Binding Type.IsUInt24}" />
    					<ToggleButton KeyboardNavigation.IsTabStop="False" Content="Signed" Height="16" FontSize="9" Margin="5,0" Padding="2,0" IsChecked="{Binding Type.IsInt24}" />
    				</StackPanel>
    				<StackPanel Orientation="Horizontal" Margin="0,1">
    					<RadioButton Name="rbInt32" KeyboardNavigation.IsTabStop="False" Content="Int32" GroupName="Type" IsChecked="{Binding Type.IsUInt}" />
    					<ToggleButton KeyboardNavigation.IsTabStop="False" Content="Signed" Height="16" FontSize="9" Margin="5,0" Padding="2,0" IsChecked="{Binding Type.IsInt}" />
    				</StackPanel>
    				<RadioButton Name="rbFloat" KeyboardNavigation.IsTabStop="False" Content="Float" GroupName="Type" Margin="0,1" IsChecked="{Binding Type.IsFloat}" />
    				<RadioButton Name="rbString" KeyboardNavigation.IsTabStop="False" Content="String" GroupName="Type" Margin="0,1" IsChecked="{Binding Type.IsString}" />
    			</StackPanel>

    		</Grid>
    	</Grid>
    </Grid>
</Window>